var version = "4.3";
var done = false;
var last_emoticon_selecter = null;

var capitalize = function(str) {
    return str[0].toUpperCase() + str.substring(1, str.length).toLowerCase();
};

var write_emoticon = function(e) {
    if (last_emoticon_selecter) {
        var input = last_emoticon_selecter.
            parentNode.parentNode.parentNode.
            previousSibling.childNodes[1].firstChild;
        if (e.src === "http://go4.es/005i.gif") {
            input.value += "(Русский)";
        } else {
            input.value += e.src;
        }
		var emoticon_selecter = document.getElementById("emoticon_selecter");
		emoticon_selecter.style.display = "none";
        input.focus();
    }
};

var add_set = function(tab) {
    //var emoticons_show = document.getElementById("emoticons_show");
	var emoticon_child_container = document.getElementById("emoticon_child_container");
    //var div = document.createElement("div");
    var table = document.createElement("table");
    var tbody = document.createElement("tbody");
    var tr = document.createElement("tr");
    var set = null;
	var prev_table_left = 0;
	
	table.style.position = "absolute";
	table.style.top = "0px";
	table.style.left = "0px";

    emoticon_child_container.innerHTML = "";
    table.appendChild(tbody);
    emoticon_child_container.appendChild(table);
	
    set = additional_sets[tab];
    for (var icon in set) {
        var td = document.createElement("td");
        var a = document.createElement("a");
        var img = document.createElement("img");

		td.style.width = "52px";
        a.className = "a_emoticon";
        a.setAttribute("href", "#");
        img.setAttribute("src", set[icon]);
        img.setAttribute("onclick", "write_emoticon(this)");

        a.appendChild(img);
        td.appendChild(a);
        tr.appendChild(td);
        
        // Each six emoticons insert a new row
        if ((parseInt(icon, 10) !== 0) && (!((parseInt(icon, 10) + 1) % 6))) {
            tbody.appendChild(tr);
            tr = document.createElement("tr");
			
			// A new table for each three rows
			if (!((parseInt(icon, 10) + 1) % 18)) {
				prev_table_left = parseInt(table.style.left.substring(0, table.style.left.length - 2), 10);
				table = document.createElement("table");
				table.style.position = "absolute";
				table.style.top = "0px";
				table.style.left = (336 + prev_table_left) + "px";
				
				tbody = document.createElement("tbody");
				table.appendChild(tbody);
				emoticon_child_container.appendChild(table);
			}
        }
		
		//if ((parseInt(icon, 10) !== 0) && (!((parseInt(icon, 10) + 1) % 21))) {
		//}
    }

    tbody.appendChild(tr);
};

var add_emoticons = function(e) {
    last_emoticon_selecter = e; 
    if (done) { return; }

    // I need to wait to emoticon_selecter to load, it's plurk.com responsability
    setTimeout(function() {
		var emoticon_pages_parent = document.getElementById("emoticon_pages_parent");
        var emoticon_selecter = document.getElementById("emoticon_selecter");
        var emoticons_tabs = document.getElementById("emoticons_tabs");
        var emoticons_tabs_ul = emoticons_tabs.childNodes.item(2);
		if (!emoticons_tabs_ul) {
			emoticons_tabs_ul = emoticons_tabs.childNodes.item(1);
		}
		
		emoticons_tabs.style.height = "auto";		
        //emoticon_selecter.style.width = "600px";
		//emoticon_pages_parent.style.width = "546px";

        var emoticon_selecter_tabs_li = emoticons_tabs_ul.childNodes;
        for (var i = 0; i < emoticon_selecter_tabs_li.length; i++) {
            emoticon_selecter_tabs_li[i].style.padding = "3px 2px";
            emoticon_selecter_tabs_li[i].style.marginLeft = "3px";
            emoticon_selecter_tabs_li[i].style.fontFamily = "Helvetica";
            emoticon_selecter_tabs_li[i].firstElementChild.style.fontSize = "8pt";
        }

        for (var set_name in additional_sets) {
            var emoticons_tabs_ul_li = document.createElement("li");
            var emoticons_a = document.createElement("a");
            emoticons_a.style.fontSize = "8pt";
            emoticons_a.href = "#";
            emoticons_a.appendChild(document.createTextNode(capitalize(set_name) + " set"));
            emoticons_tabs_ul_li.appendChild(emoticons_a);
            emoticons_tabs_ul_li.className = "emoticon_selecter";
            emoticons_tabs_ul_li.id = "emo_" + set_name;
            emoticons_tabs_ul_li.style.padding = "3px 2px";
            emoticons_tabs_ul_li.style.marginLeft = "3px";
            emoticons_tabs_ul.appendChild(emoticons_tabs_ul_li);
            emoticons_a.onclick = function() {
                if (emoticons_tabs_ul.hasChildNodes()) {
                    for (var i in emoticons_tabs_ul.childNodes) {
                        emoticons_tabs_ul.childNodes[i].className = "emoticon_selecter";
                    }
                }
                this.parentNode.className = "emoticon_selecter current";
                add_set(this.innerHTML.toLowerCase().substring(0, this.innerHTML.length - 4));
                return false;
            }; 
        }		
        done = true;
    }, 200);
};

window.setTimeout(function() { 
    emoticon_selecter_img = document.getElementsByClassName("emoticon_selecter_img_off");
    top_login = document.getElementById("top_login");

    for (var i in emoticon_selecter_img) {
        try {
            emoticon_selecter_img[i].addEventListener("click", function() { add_emoticons(this); }, true);
        } catch(e) {}
    }

    if (top_login) {
        new_a = document.createElement("a");
        new_a.href = "http://pifeb.googlecode.com/hg/plurkiconsforeverybody.user.js";
        new_a.appendChild(document.createTextNode("PIFEB " + version));
        top_login.appendChild(new_a);
    }
}, 4000);
